package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageBean;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    @PutMapping
    public AjaxResult saveorupdate(@RequestBody Department department) {
        System.out.println("来了");
        try {
            if (department.getId() == null) {
                service.add(department);
            } else {
                service.update(department);
            }
        } catch (Exception e) {
            return new AjaxResult(false, "操作失败");
        }
        return new AjaxResult();
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            service.del(id);
        } catch (Exception e) {
            return new AjaxResult(false, "操作失败");
        }
        return new AjaxResult();
    }

    @PostMapping
    public PageBean<Department> fenye(@RequestBody DepartmentQuery query) {
        return service.queryhigt(query);
    }

    @GetMapping("/find")
    public List<Department> findall() {
        List<Department> list=service.qaueryall();
        return service.qaueryall();
    }

    @GetMapping("/{id}")
    public Department fingbuid(@PathVariable("id") Long id) {
        return service.querybyid(id);
    }

    @PostMapping("/del")
    public AjaxResult delList(@RequestBody List<Department> departments){
        try {
            for (Department de:departments) {
                service.del(de.getId());

            }
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false,e.getMessage());
        }
    }
    @PostMapping("/tree")
    public List<Department> deleo(){
        List<Department> list=service.selechid();
        return service.selechid();
    }

}
